package com.qjcc.app;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

import java.util.List;

/**
 * Application 类
 *
 */
public class AppContext extends Application {
    private static AppContext ins;
	public static String CURRENT_USER = "0";// 登录用户的id
	public static AppContext leaveAppContext;
	@Override
    public void onCreate() {
    	super.onCreate();
    	ins = this;
    }
	
	public static AppContext getAppContext() {
		return ins;
	}
	
	/**
	 * 获取App安装包信息
	 * @return
	 */
	public PackageInfo getPackageInfo() {
		PackageInfo info = null;
		try { 
			info = getPackageManager().getPackageInfo(getPackageName(), 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace(System.err);
		} 
		if(info == null) info = new PackageInfo();
		return info;
	}
	
	/**
	 * 检查程序是否处于前台活动状态
	 * @param context
	 * @return
	 */
	public static boolean isAction(final Context context) {
	       ActivityManager am = (ActivityManager)AppContext.getAppContext().getSystemService(Context.ACTIVITY_SERVICE);
	       List<RunningTaskInfo> tasks = am.getRunningTasks(1);
	       if (!tasks.isEmpty()) {
	             ComponentName topActivity = tasks.get(0).topActivity;
	             if (topActivity.getPackageName().equals(context.getPackageName())) {
	                   return true;
	             }
	       }
	     return false;
	}
}
